package com.flycent.fanbao.ui;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.FinalDb;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.flycent.fanbao.App;
import com.flycent.fanbao.Constants;
import com.flycent.fanbao.R;
import com.flycent.fanbao.adapter.AbsViewHolderAdapter;
import com.flycent.fanbao.bean.Ask;
import com.google.gson.Gson;

public class AskActivity extends BackableTitleBarActivity{
	@ViewInject(id = R.id.ask_list , itemClick = "itemClick") ListView mList;
	@ViewInject(id = R.id.ask_question_edit) EditText mQuestionEdit;
	@ViewInject(id = R.id.ask_submit_but ,click = "onClick") TextView mSubmit;

	private AskAdapter mAdapter;
	private Ask mAsk;
	private List<Ask> mAsks = new ArrayList<Ask>();
	private FinalDb db;

	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_ask);
		FinalActivity.initInjectedView(this);
		getTitleBar().setTitle("趣味问答");

		db = FinalDb.create(this);

		mAdapter = new AskAdapter(this, R.layout.item_ask_list);
		mList.setAdapter(mAdapter);

		mAsks = db.findAll(Ask.class);
		mAdapter.update(mAsks);
		mList.setSelection(mAdapter.getData().size());

	}

	public void onClick(View v){
		if( v == mSubmit){
			SubmitProblem(getText(mQuestionEdit));
			mQuestionEdit.setText("");
		}	
	}

	public void itemClick(AdapterView<?> parent , View view , int position,long id){
		if(parent ==  mList){

		}
	}

	public class AskAdapter extends AbsViewHolderAdapter<Ask>{

		public AskAdapter(Context context, int layoutRes) {
			super(context, layoutRes);
		}

		@Override
		protected void bindData(int pos, Ask itemData) {
			TextView leftText = getViewFromHolder(R.id.left_text);
			TextView rightText = getViewFromHolder(R.id.right_text);

			if( pos %2 == 0){
				rightText.setVisibility(View.GONE);
				leftText.setVisibility(View.VISIBLE);
				leftText.setText(itemData.getContent());
			}else {
				leftText.setVisibility(View.GONE);
				rightText.setVisibility(View.VISIBLE);
				rightText.setText(itemData.getContent());
			}
		}
	}
	private void SubmitProblem(String str){

		db.save(new Ask(str));
		mAsks = db.findAll(Ask.class);
		mAdapter.update(mAsks);
		mList.setSelection(mAdapter.getData().size());
//http://api.ajaxsns.com/api.php?key=free&appid=0&msg=%E4%BD%A0%E5%A5%BD

		FinalHttp fh  = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("key", "free");
		params.put("appid", "0");
		
//		params.put("info", str);
//		params.put("loc", "");
//		params.put("lon", "");
//		params.put("lat", "");
//		params.put("userid", "");
		
		params.put("msg", str);
		
		fh.get(Constants.AskIP, params, new AjaxCallBack<Object>() {

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				App.showLog("strMsg=="+strMsg);
			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				App.showLog("t=="+t.toString());

				try {
					JSONObject jsonObject = new JSONObject(t.toString());
					if(jsonObject.getInt("result") == 0 ){
						Gson gson = new Gson();
						mAsk = gson.fromJson(jsonObject.toString(), 
								Ask.class);

						db.save(new Ask(mAsk.getContent()));
						
						mAsks = db.findAll(Ask.class);
						mAdapter.update(mAsks);
						mList.setSelection(mAdapter.getData().size());

					}else {


					}
				} catch (Exception e) {
					e.printStackTrace();
				}




			}

		});


	}

}
